﻿#ifndef TCPREQUESTER_H
#define TCPREQUESTER_H
#include "myhead.h"
#include "baserequester.h"

#include <QObject>
#include <QTcpSocket>
#include <QTimer>
#include <QSemaphore>
#include <QMutex>
#include <QMutexLocker>

class TcpRequester : public BaseRequester
{
    Q_OBJECT
public:
    Q_INVOKABLE explicit TcpRequester(QObject *parent = nullptr);
    ~TcpRequester();

    // BaseCommunicationZZZ interface
public:
    virtual bool connectToHost() override;
    virtual bool isConnected() override;
    virtual bool disConnect() override;

    virtual QByteArray request(QByteArray data) override;

    virtual bool requestNoReply(QByteArray data) override;

signals:

public slots:
    void onReadyReady();
    void onError1(QAbstractSocket::SocketError err);

private:
    QTcpSocket *_socket=nullptr;
    QMutex mutex;
    QList<QByteArray> _recList;

    bool requestImpl(QByteArray sendBuff, QByteArray &receiveBuff, int timeoutMs);

    void waitMs(int ms);

public:
    virtual QWidget *getConfigWidget() override;
};

#endif // TCPREQUESTER_H
